<?php

use App\Util\Helper;
use Plugins\AdminBasic\Enums\BasicRouteEnumOpenType;
use Plugins\AdminBasic\Enums\BasicRouteEnumType;
use Plugins\AdminBasic\Http\Admin\Controller\RouteController;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\HtmlStructure\Html\Js;
use Sc\Util\HtmlStructure\Html\Js\JsCode;
use Sc\Util\HtmlStructure\Html\Js\JsFunc;
use Sc\Util\HtmlStructure\Table;

Html::create('路由列表');

$table = Table::create(Helper::route()->to([RouteController::class, 'listsData'])->url(), 'route');

$table->setAttr('v-if', Html::js()->vue->bind('show', true))->addColumns(
    Table\Column::selection(),
    Table\Column::normal('标题', 'title')->width(200, false),
    Table\Column::normal('类型', 'type')->width(100)->showTags(BasicRouteEnumType::tagsMapping()),
    Table\Column::normal('打开方式', 'open_type')->width(100)->showTags(BasicRouteEnumOpenType::tagsMapping()),
    Table\Column::normal('路由权重', 'weigh')->width(80),
    Table\Column::normal('图标', 'icon')->width(80)->setFormat('<el-icon><component :is="icon"></component></el-icon>'),
    Table\Column::normal('路由地址', 'route')->align('left'),
    Table\Column::event()->width(150),
);

$component = include __DIR__ . '/edit.sc.php';

$table->setHeaderEvent('@primary.Plus.新增',
    Table\EventHandler::window('新增路由')->setConfig(['width' => '500px'])->setContent($component)
);

$table->setRowEvent('@primary.Edit.编辑',
    Table\EventHandler::window('编辑路由')->setConfig(['width' => '500px'])->setContent($component)
);

$table->setHeaderEvent("@danger.delete.删除",
    Table\EventHandler::post(
        url: Helper::route()->to([RouteController::class, 'destroy']),
        data: ['ids' => '@selection.map(v => v.id)'],
        successHandler: JsCode::create("this.\$message.success('删除成功')")->then("this.routeGetData()")
    )->confirmMessage('确认删除选中数据吗？')
        ->addLoading()
);

$table->setAttr([
    'row-key' => 'id'
]);

$table->setPagination(false);
$table->setDraw('@primary.rank.排序', function (){
    return JsFunc::call("setTimeout", JsFunc::arrow()->code(
        Js::var('newData', '@this.getTreeData(this.route, evt.newIndex, 0)'),
        Js::var('oldData', '@this.getTreeData(this.route, evt.oldIndex, 0)'),
        Js::var('isUp', '@evt.oldIndex < evt.newIndex'),
        Js\Axios::post(Helper::route()->to([RouteController::class, 'sort']), [
            'oldId' => '@oldData.id',
            'newId' => '@newData.id',
            'isUp' => '@isUp',
        ])->success(Js::code(
            Js::assign('this.show', false),
            Js::code('this.routeGetData()'),
            Js::code('this.$nextTick(() => {this.show = true;this.$nextTick(() => this.routeInitSort()) })'),
        ))
    ));
});

Html::js()->vue->addMethod('getTreeData', JsFunc::anonymous(['data', 'index', 'cur'])->code(
    Js::for('let i = 0; i < data.length; i++')->then(
        Js::if('index === cur')->then(
            Js::code('return data[i]')
        ),
        Js::assign('cur', '@cur + 1'),
        Js::if("data[i].hasOwnProperty('children')")->then(
            Js::var('r', '@this.getTreeData(data[i].children, index, cur)'),
            Js::if('r && isNaN(r)')->then(
                Js::code('return r')
            )->else(
                Js::assign('cur', '@r')
            ),
        ),
    ),
    Js::code('return cur')
));


return $table->render();
